package com.bestom.waterquality.logic;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.bestom.waterquality.R;
import com.bestom.waterquality.entity.Constant;
import com.bestom.waterquality.entity.RealBean;
import com.bestom.waterquality.sql.DbManage;

import java.util.ArrayList;

/**
 * 图像查看
 */
public class ImageViewer {

    private Activity mActivity;
    private Context mContext;
    private Handler mHandler;
    private ArrayList<RealBean> realList;
    private ImageViewer() {
    }

    private static ImageViewer _instance = null;

    public static ImageViewer getInstance() {
        if (_instance == null)
            _instance = new ImageViewer();
        return _instance;
    }

    public void setDataAndListen(Activity activity, Context context, View view, Handler handler){

        if (mContext == null){
            mActivity = activity;
            mContext = context;
            mHandler = handler;
            getDataFromDb();
            ListView listView = view.findViewById(R.id.real_list);
            listView.setAdapter(mRealBaseAdapter);
        }
    }

    public void addRealData(RealBean bean){
        realList.add(0,bean);
        mActivity.runOnUiThread(mRealBaseAdapter::notifyDataSetChanged);
    }

    private void getDataFromDb(){
        realList = new ArrayList<>();
        Cursor cursor = DbManage.getInstance(mContext).select("real_record");
        Log.d("execute", "cursor2 --->" + cursor.getCount());
        while (cursor.moveToNext()) {
            String time = cursor.getString(cursor.getColumnIndex("time"));
            String vcp = cursor.getString(cursor.getColumnIndex("real_vcp"));
            String tmp = cursor.getString(cursor.getColumnIndex("real_tmp"));
            String path = cursor.getString(cursor.getColumnIndex("image_path"));
            RealBean bean = new RealBean(time, vcp, tmp, path);
            realList.add(bean);
        }
        cursor.close();
    }

    private final BaseAdapter mRealBaseAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return realList.size();
        }

        @Override
        public Object getItem(int position) {
            return realList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            View view;
            if (convertView == null) {
                LayoutInflater inflater = mActivity.getLayoutInflater();
                view = inflater.inflate(R.layout.real_item, null);
            } else {
                view = convertView;
            }
            //warningList，position相当于数组下标,可以实现逐行取数据
            RealBean bean = realList.get(position);
            TextView time = view.findViewById(R.id.real_time);
            TextView vcp = view.findViewById(R.id.real_vcp);
            TextView tmp = view.findViewById(R.id.real_tmp);
            TextView path = view.findViewById(R.id.real_path);

            time.setText(bean.getTime());
            vcp.setText(bean.getVcp());
            tmp.setText(bean.getTmp());
            path.setText(bean.getPath());
            path.setOnClickListener(view1 -> {
                if (bean.getPath().isEmpty()) return;
                Message message = new Message();
                message.obj = Constant.realImageDir + bean.getPath();
                mHandler.sendMessage(message);
            });

            return view;
        }
    };
}
